interface watermark {
  set: (str: string) => void,
}

let watermark: watermark = {
  set: () => {
    console.log('请使用watermark.set(str)方法设置水印');
  },
}


let setWatermark = (str: string) => {
  let id = '1.23452384164.123412416';

  if (document.getElementById(id) !== null) {
    document.body.removeChild(document.getElementById(id) as HTMLElement);

  }

  //创建一个画布
  let can = document.createElement('canvas');
  //设置画布的长宽
  can.width = 250;
  can.height = 150;

  let cans = can.getContext('2d');
  //旋转角度
  if (cans) {
    cans?.rotate(-30 * Math.PI / 180);
    cans.font = '24px Vedana';
    cans.fillStyle = 'rgba(200, 200, 200, 0.35)';
    cans.textAlign = 'left';
    cans.textBaseline = 'middle';
  }
  // 控制多少

  cans && cans.fillText(str, can.width / 200 - 50, can.height - 22,);
  let div = document.createElement('div');
  div.id = id;
  div.style.pointerEvents = 'none';
  div.style.top = '10px';
  div.style.left = '-40px';
  div.style.position = 'fixed';
  div.style.zIndex = '1000';
  div.style.width = document.documentElement.clientWidth + 'px';
  div.style.height = document.documentElement.clientHeight + 'px';
  div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat';
  document.body.appendChild(div);
  return id;
}

// 该方法只允许调用一次
watermark.set = (str) => {
  let id = setWatermark(str);
  setInterval(() => {
    if (document.getElementById(id) === null) {
      id = setWatermark(str);
    }
  }, 500);
  window.onresize = () => {
    setWatermark(str);
  };
}

export default watermark;
